<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #ball{
            width: 100px;
            height: 100px;
            background-color: red;
            border-radius:50%;
            margin-left: 0px;
        }
        #ball2{
            width: 100px;
            height: 100px;
            background-color: red;
            border-radius:50%;
            margin-left: 0px;
        }
    </style>
</head>
<body>
<div id="ball"></div>
<div id="ball2"></div>
<input type="button" value="球1" id="btn1" >
<input type="button" value="球2" id="btn2" >
<script>
    var btn1 = document.getElementById('btn1');
    var btn2 = document.getElementById('btn2');
    var ball = document.getElementById('ball');
    var ball2 = document.getElementById('ball');
    function go(fps){//frame per second
        var t;//闭包
        function myMove(id,end){
            var htmlObj = document.getElementById(id);
            clearInterval(t);
            var offset = 1;     //速度
            function move(){
                var cssObj = getComputedStyle(htmlObj);
                var originL = parseInt(cssObj.marginLeft);
                if (originL>=end){
                    clearInterval(t);
                } else {
                    htmlObj.style.marginLeft = originL + offset + 'px';
                }
            }
            var m = 1000/fps; //
            t = setInterval(move,m);//1
        }
        return myMove;
    }
    var move1 = go(50);
    var move2 = go(100);
//    var move2 = go(100);
    //onclick="move2('ball2',800)"
    //onclick="move1('ball',500)"
btn1.onclick = function(){
    move1('ball',500);
};
btn2.onclick = function(){
    move2('ball2',800);
};







</script>

</body>
</html>